using PKGames;
using PKGames.SceneManagement;
//using PKGames.Store;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using VoxelEngine;

namespace VoxelGame
{
	public class WorldSelectionCell : TapAction, IGridCell
	{
		[SerializeField]
		private Image _iconImage;

		[SerializeField]
		private AspectRatioFitter _aspectRatioFitter;

		[SerializeField]
		private GameObject _trialOverlay;

		[SerializeField]
		private GameObject _lockedOverlay;

		[SerializeField]
		private Text _priceText;

		private WorldSelectionCellData _cellData;

		private Action _onEnterCallback;

		private Action _onExitCallback;

		private Action _onBlockInput;

		private IEnumerator _updateCoroutine;

		public IGridCellData CellData
		{
			get
			{
				return _cellData;
			}
			set
			{
				_cellData = (WorldSelectionCellData)value;
			}
		}

		public Transform Transform => base.transform;

		public override void Start()
		{
			base.Start();
			UpdateCellOverlay();
		}

		private void OnDestroy()
		{
			_cellData.WorldSelectionConfig.Dispose();
		}

		private void UpdateCellOverlay()
		{
			switch (_cellData.WorldSelectionConfig.CurrentWorldState)
			{
			case WorldSelectionConfig.WorldState.Free:
				SetCellUnlockedState();
				break;
			case WorldSelectionConfig.WorldState.Trial:
				SetCellTrialState();
				break;
			case WorldSelectionConfig.WorldState.Locked:
				SetCellLockedState();
				break;
			case WorldSelectionConfig.WorldState.Unlocked:
				SetCellUnlockedState();
				break;
			default:
				throw new ArgumentOutOfRangeException();
			}
		}

		private void SetCellUnlockedState()
		{
			_trialOverlay.SetActive(value: false);
			_lockedOverlay.SetActive(value: false);
		}

		private void SetCellTrialState()
		{
			_trialOverlay.SetActive(value: true);
			_lockedOverlay.SetActive(value: false);
		}

		private void SetCellLockedState()
		{
			_trialOverlay.SetActive(value: false);
			_lockedOverlay.SetActive(value: true);
			//StartCoroutine(GetPriceStringAsync());
		}

		//private IEnumerator GetPriceStringAsync()
		//{
		//	yield return StoreManager.Instance.WaitForInit();
		//	_priceText.text = GetPriceText();
		//}

		private string GetPriceText()
		{
			//if (_cellData.WorldSelectionConfig.Product != null)
			//{
			//	return StoreManager.Instance.GetCurrencyForProduct(_cellData.WorldSelectionConfig.Product) + " " + StoreManager.Instance.GetPriceForProduct(_cellData.WorldSelectionConfig.Product);
			//}
			return string.Empty;
		}

		public void UpdateCell(IGridCellData CellData)
		{
			this.CellData = CellData;
			if (_cellData != null)
			{
				_updateCoroutine = null;
				_updateCoroutine = UpdateCell();
				StartCoroutine(_updateCoroutine);
			}
		}

		public void UpdateCell(IGridCellData CellData, Action onEnterCallback, Action onExitCallback)
		{
			_onEnterCallback = onEnterCallback;
			_onExitCallback = onExitCallback;
			UpdateCell(CellData);
		}

		public IEnumerator UpdateCell()
		{
			LoadSpriteIcon();
			UpdateCellOverlay();
			yield return null;
		}

		private void LoadSpriteIcon()
		{
			if (_iconImage.sprite != _cellData.IconTexture)
			{
				_iconImage.sprite = _cellData.IconTexture;
			}
		}

		protected override IEnumerator ExecuteAfterButtonAnimation()
		{
			switch (_cellData.WorldSelectionConfig.CurrentWorldState)
			{
			case WorldSelectionConfig.WorldState.Free:
				OpenSceneAction();
				break;
			case WorldSelectionConfig.WorldState.Trial:
				OpenSceneAction();
				break;
			case WorldSelectionConfig.WorldState.Locked:
				ShowIapCreative();
				break;
			case WorldSelectionConfig.WorldState.Unlocked:
				OpenSceneAction();
				break;
			default:
				throw new ArgumentOutOfRangeException();
			}
			yield return null;
		}

		private void OpenSceneAction()
		{
            MainMenuToggleController.Instance.checkTouch.action = () =>
            {
                _onEnterCallback.Fire();
                BaseWorldController.VoxelPlayerData = _cellData.WorldSelectionConfig;
                BaseWorldController.OnDisableAction = _onExitCallback;
                MonoSingleton<SceneLoader>.Instance.LoadSubScene("VoxelPlayer");
                AdManager.ShowNative();
                if (!AdManager1.instance.ShieldAddress) AdManager.ShowVideo(() => { });
            };
            MainMenuToggleController.Instance.checkTouch.gameObject.SetActive(true);
		}

		private void ShowIapCreative()
		{
			//MonoSingleton<IapCreativeManager>.Instance.SetupIapCreative(_cellData.IconTexture, _cellData.WorldSelectionConfig.Product, _cellData.WorldSelectionConfig.VoxelWorldData.WorldId);
			//MonoSingleton<IapCreativeManager>.Instance.ShowIapCreative(IapCreativeUiController.IapTrigger.LevelLocked, 0.15f, OnCreativeCloseAction);
		}

		private void OnCreativeCloseAction()
		{
			//MonoSingleton<IapCreativeManager>.Instance.CloseIapCreative();
		}

		private void OnApplicationPause(bool pauseStatus)
		{
			if (pauseStatus)
			{
				_onExitCallback.Fire();
			}
			else
			{
				_onEnterCallback.Fire();
			}
		}
	}
}
